#include "qtemplatetreewidget.h"

#include "qtemplatenamewidget.h"

#include "../../libs/sharedLib/qrenamedialog.h"

#include <QItemDelegate>
#include <QHeaderView>
#include <QPainter>
#include <QMessageBox>
#include <QFile>


class QTemplateDeletgate: public QItemDelegate
{
public:
    QTemplateDeletgate(QTemplateTreeWidget* view):m_view(view){}

    QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    QSize   sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
    void    paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
protected:
    QTemplateTreeWidget *m_view;
};


void QTemplateDeletgate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItemV3 opt=option;
    QColor c;
    QTreeWidgetItem *item=m_view->itemFromIndex(index);
    if(item==m_view->m_default || item==m_view->m_more)
    {
        c=QColor(128,128,128);
    }
    else
    {
        c=QColor(255,220,150);
        if(opt.features & QStyleOptionViewItemV2::Alternate)
        {
            c=c.lighter(132);
        }
    }

    painter->fillRect(option.rect,c);
    opt.state &=~ QStyle::State_HasFocus;
    QItemDelegate::paint(painter,opt,index);
}

QSize QTemplateDeletgate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSize sz=QItemDelegate::sizeHint(option,index);
    sz.setHeight(20);
    return sz;
}

QWidget* QTemplateDeletgate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QTemplateNameWidget* wid=NULL;
    return wid;
}



QTemplateTreeWidget::QTemplateTreeWidget(QWidget *parent) :
    QTreeWidget(parent),
    m_default(new QTreeWidgetItem(this)),
    m_more(NULL)
{
    this->setItemDelegate(new QTemplateDeletgate(this));
    this->setAlternatingRowColors(true);
    this->header()->hide();

    m_default->setFlags(Qt::ItemIsEnabled);

    m_default->setText(0,tr("Template"));
    this->setItemExpanded(m_default,true);

    QPalette p=this->palette();
    p.setColor(QPalette::Inactive,QPalette::Highlight,p.color(QPalette::Active,QPalette::Highlight));
    p.setColor(QPalette::Inactive,QPalette::HighlightedText,p.color(QPalette::Active,QPalette::HighlightedText));
    this->setPalette(p);

    connect(this,SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),this,SLOT(current_Item(QTreeWidgetItem*,QTreeWidgetItem*)));
}


void QTemplateTreeWidget::add_default_item(const QString &file_name, const QString &name)
{


    QTreeWidgetItem *item=m_nameToItem.value(file_name);
    if(item!=NULL)
    {
        return;
    }

    item=new QTreeWidgetItem(m_default);
    item->setToolTip(0,name);
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled);
    tagTemplateInfo* info=new tagTemplateInfo;
    info->m_file_name=file_name;
    info->m_name=name;
    m_default_items.insert(item,info);
    m_nameToItem.insert(file_name,item);

    QTemplateNameWidget *wid=new QTemplateNameWidget;
    wid->setText(name);
    wid->set_tree_item(this,item);
    wid->set_up_button_visible(false);
    this->setItemWidget(item,0,wid);

    if(this->currentItem()==NULL)
    {
        setCurrentItem(item);
    }
}

void QTemplateTreeWidget::add_more_item(const QString &file_name, const QString &name)
{
    QTreeWidgetItem *item=m_nameToItem.value(file_name);
    if(item!=NULL)
    {
        return;
    }
    if(m_more==NULL)
    {
        m_more=new QTreeWidgetItem(this);
        m_more->setText(0,tr("Custom Files"));
        m_more->setFlags(Qt::ItemIsEnabled);
        this->setItemExpanded(m_more,true);
    }
    item=new QTreeWidgetItem(m_more);
    item->setToolTip(0,file_name);
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled);
    tagTemplateInfo* info=new tagTemplateInfo;
    info->m_file_name=file_name;
    info->m_name=name;
    m_more_items.insert(item,info);
    m_nameToItem.insert(name,item);

    QTemplateNameWidget *wid=new QTemplateNameWidget;
    wid->setText(name);
    wid->set_tree_item(this,item);
    wid->set_up_button_visible(true);
    this->setItemWidget(item,0,wid);

    setCurrentItem(item);
}

void QTemplateTreeWidget::drawRow(QPainter *painter, const QStyleOptionViewItem &options, const QModelIndex &index) const
{
    QStyleOptionViewItemV3 opt=options;
    QTreeWidgetItem* item=itemFromIndex(index);
    QColor c;
    if(item==m_default || item==m_more)
    {
        c=QColor(128,128,128);
        painter->fillRect(options.rect,c);
        opt.palette.setColor(QPalette::AlternateBase,c.lighter(112));
    }
    else
    {
        c=QColor(255,220,150);
        painter->fillRect(options.rect,c);
        opt.palette.setColor(QPalette::AlternateBase,c.lighter(132));
    }

    opt.state &=~ QStyle::State_HasFocus;
    QTreeWidget::drawRow(painter,opt,index);
}

void QTemplateTreeWidget::current_Item(QTreeWidgetItem *current, QTreeWidgetItem *old)
{
    tagTemplateInfo *info=m_default_items.value(current);
    if(info!=NULL)
    {
        emit select(info);
        return;
    }
    else
    {
        info=m_more_items.value(current);
        if(info!=NULL)
        {
            emit select(info);
            return;
        }
    }

    emit select(NULL);
}

void QTemplateTreeWidget::remove(QTreeWidgetItem *item)
{
    if(m_default_items.keys().contains(item))
    {
        if(QMessageBox::warning(this,tr("Remove"),tr("Do you want to delete this template?"),QMessageBox::Yes | QMessageBox::No)!=QMessageBox::Yes)
        {
            return;
        }
    }
    if(item==this->currentItem())
    {
        if(m_default_items.size()>1)
        {
            QTreeWidgetItem *temp=m_default->child(0);
            if(temp==item)
            {
                temp=m_default->child(1);
            }
            setCurrentItem(temp);
        }
        else
        {
            setCurrentItem(NULL);
        }
    }
    if(m_default_items.keys().contains(item))
    {
        tagTemplateInfo *info=m_default_items.value(item);
        QFile f(info->m_file_name);
        f.remove();
        m_default_items.remove(item);
        m_nameToItem.remove(info->m_file_name);
        emit remove_signal(info->m_file_name);
        delete info;
        delete item;
    }
    else
    {
        tagTemplateInfo *info=m_more_items.value(item);
        m_more_items.remove(item);
        m_nameToItem.remove(info->m_file_name);
        emit remove_signal(info->m_file_name);
        delete info;
        delete item;
        if(m_more_items.size()==0)
        {
            delete m_more;
            m_more=NULL;
        }
    }

}

void QTemplateTreeWidget::movetotemp(QTreeWidgetItem *item)
{
    if(m_more_items.keys().contains(item))
    {
        tagTemplateInfo *info=m_more_items.value(item);
        QString path=m_base_file_path+info->m_name+".xml";
        QString name=info->m_name;
        if(m_nameToItem.value(path)!=NULL)
        {
            if(QMessageBox::warning(this,tr("Warning"),tr("File name not available! Do you want to rename it?"),QMessageBox::Yes | QMessageBox::No)!=QMessageBox::Yes)
            {
                return;
            }

            SHAREDLIB::QRenameDialog dialog(this);
            QStringList list;
            QMapIterator<QTreeWidgetItem*,tagTemplateInfo*>    it(m_default_items);
            while(it.hasNext())
            {
                it.next();
                list.append(it.value()->m_name);
            }
            dialog.set_name_list(list);
            dialog.set_old_name(name);
            dialog.exec();
            if(dialog.get_ret()==1)
            {
                name=dialog.get_new_name();
            }
            else
            {
                name="";
            }

        }
        if(name=="")
        {
            return;
        }
        path=m_base_file_path+"/"+name+".xml";

        if(!QFile::copy(info->m_file_name,path))
        {
            return;
        }
        emit changed(info->m_file_name,path);
        remove(item);
        add_default_item(path,name);
        setCurrentItem(m_nameToItem.value(path));
    }
}

void QTemplateTreeWidget::set_base_path(const QString &base_path)
{
    m_base_file_path=base_path;
}
